
/**
 * 浏览器地址参数解析
 * @param {*} name 
 * @returns 
 */
export const getQueryString = (name) => {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
};

/**
 * 接口请求统一处理函数
 * @param {*} apiFn 
 * @param {*} params 
 * @returns 
 */
export const apiFn = (apiFn, params) => {
    return new Promise((resolve, reject) => {
        try {
            apiFn(params).then(res => {
                const { code } = res;
                if(code === '200' || code === 'success' || code === '0'){
                    resolve(res);
                }else{
                    reject(res);
                }
            }).catch(error => {
                reject(error);
            })
        } catch (error) {
            reject(error);
            throw new Error (error);
        }
    });
};

/**
 * 数据深拷贝
 * @param {*} data 
 * @returns 
 */
export const deepCopy = (data) => {
    return JSON.parse(JSON.stringify(data));
};

/**
 * 导出报表文件
 * @param {*} url 
 * @param {*} type 
 * @param {*} option 
 */
export const exportReportFile = (url, type, option) => {
    const xhr = new XMLHttpRequest();
    const headerType = type == 'POST'? 'application/json' : "application/x-www-form-urlencoded";
    xhr.open(type, url, true);
    xhr.setRequestHeader(
        "Content-Type",
        headerType
    ); 
    xhr.responseType = "blob"; 
    xhr.setRequestHeader("token", option.token);
    xhr.onload = function () {
        if (this.status == 200) {
            try {
                let blob = this.response;
                let name = xhr.getResponseHeader("content-disposition");
                if (name == null) {
                    let resultMsg = xhr.getResponseHeader("resultMsg");
                    let msgStr = resultMsg ||'暂无数据';
                    if (resultMsg == "1") {
                        msgStr = "无法导出,查询数据为空";
                    }
                    option['ElMessage'].warning(msgStr);
                    return;
                }
                let reader = new FileReader();
                reader.readAsDataURL(blob); // 转换为base64，可以直接放入a标签href
                reader.onload = function (e) {
                    let a = document.createElement("a"); // 转换完成，创建一个a标签用于下载
                    a.download = decodeURI(name);
                    a.href = window.URL.createObjectURL(blob);
                    a.click();
                    a.remove();
                };
            } catch (e) {
                option['ElMessage'].error("文件导出失败！");
            }
        }
    };
    if(option.params){
        xhr.send(JSON.stringify(option.params));
    }else{
        xhr.send()
    }
};

/**
 * 表格添加合并单元格数据
 * @param {*} datas 
 * @returns 
 */
export const getObjectSpan = (datas) => {
    let objectSpan = {};
    datas.forEach((data) => {
        const { vehicleModel, tyDate, org } = data;
        if (vehicleModel) {
            const key = `${vehicleModel}_${tyDate}_${org}`;
            if (key in objectSpan) {
                objectSpan[key]["rowspan"] += 1;
                data.rowspan = 0;
                data.colspan = 0;
            } else {
                data.rowspan = 1;
                data.colspan = 1;
                objectSpan[key] = data;
            }
        }
    });
    return datas;
};